﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Expression.Blend.SampleData.TourThaiData;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media;

namespace TourThailand
{
    public partial class LocationRecommendPage : PhoneApplicationPage
    {
        GeoCoordinateWatcher gps;
        public LocationRecommendPage()
        {
            InitializeComponent();

            gps = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            gps.Start();
            gps.StatusChanged += gps_StatusChanged;
            gps.PositionChanged += gps_PositionChanged;
            MapLayer layer = new MapLayer();
            MapOverlay overlay = new MapOverlay();
            Button btn = new Button() { Content = "Hello" };
            btn.Background = new SolidColorBrush(Colors.Green);
            btn.Click += btn_Click;
            overlay.Content = btn;
            overlay.GeoCoordinate = new GeoCoordinate(18.79000, 098.9849);

            layer.Add(overlay);
            myMap.Layers.Add(layer);
            myMap.Center = new GeoCoordinate(18.79000, 098.9849);
            myMap.ZoomLevel = myMap.ZoomLevel * 6;

        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello");
        }
		
		protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            App app = App.Current as App;
          
            myGrid.DataContext = (LocationRecommendCollectionItem)app.passingObject;
           
            WebClient web = new WebClient();
            String url = String.Format("http://i.wxbug.net/REST/Direct/GetForecast.ashx?la={0}&lo={1}2&nf=7&ih=0&ht=i&l=th&c=TH&units=1&api_key=mxf782bm8qdy2chx25sn6ej2",13.9037,100.5291);
            web.DownloadStringAsync(new Uri(url));
            web.DownloadStringCompleted += web_DownloadStringCompleted;
        }

        void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
			
            RootObject json = JsonConvert.DeserializeObject<RootObject>(e.Result);
            WeatherLongList.ItemsSource = json.forecastList;

        }
        
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
           

            switch (gps.Status)
            {
                case GeoPositionStatus.Disabled:
                    break;
                case GeoPositionStatus.Initializing:
                    break;
                case GeoPositionStatus.NoData:
                    MessageBox.Show("No Data");
                    break;
                case GeoPositionStatus.Ready:
                    findRuotePath();
                    break;
                default:
                    break;
            }
        }

        public void findRuotePath() {
            try
            {
                App app = App.Current as App;
                var item = (LocationRecommendCollectionItem)app.passingObject;
                MapsDirectionsTask direction = new MapsDirectionsTask();
                direction.Start = new LabeledMapLocation("I'm here", gps.Position.Location);
                direction.End = new LabeledMapLocation(item.Title, new GeoCoordinate(18.79000, 098.9849));
                direction.Show();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        void gps_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            //throw new NotImplementedException();
            if (gps.Status == GeoPositionStatus.Ready)
            {
                myMap.Center = gps.Position.Location;
            }
        }

        void gps_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }
    }
	
	public class ForecastList
    {
        public string dateTime { get; set; }
        public string dayDesc { get; set; }
        public string dayIcon { get; set; }
        public string dayPred { get; set; }
        public string dayTitle { get; set; }
        public bool hasDay { get; set; }
        public bool hasNight { get; set; }
        public string high { get; set; }
        public string hourly { get; set; }
        public string low { get; set; }
        public string nightDesc { get; set; }
        public string nightIcon { get; set; }
        public string nightPred { get; set; }
        public string nightTitle { get; set; }
        public string title { get; set; }
    }

    public class RootObject
    {
        public List<ForecastList> forecastList { get; set; }
        public List<int> notifications { get; set; }
        public string temperatureUnits { get; set; }
    }
}